/**
 * 格式化条形图
 * @des {
 *   仅格式化组件所需要的数据
 * }
 */
import { $each, $isEmptyArr, deepCopy } from '../index';
import { processXAis, processSeriesData  } from './lineAndBarFormat.js';
import { handleConditionExtend } from '../conditionFormat/useConditionFormat.js';
import { handleChangeGroup }  from '../changeGroup.js';
export function stripFormat(datasets) {
  const { source, dimensions, fieldNameChainMap, conditionData, conditionOptions } = datasets;
  const { yAxis, yAxisBar, yAxisCondition } = dimensions;
  const xAxisData = processXAis(datasets);
  const percentageData = [];
  const seriesData = [];
  let yAxisConditionArr = [];//用来单独存储条件项名称
  // 条件项存在多度量
  if (yAxisCondition && yAxisCondition.includes('&')) {
    yAxisConditionArr = yAxisCondition.split('&');
  } else {
    yAxisConditionArr = [yAxisCondition];
  }
  //提示信息
  $each(source, (item) => {
    const tooltip= {};
    // 分子
    const molecule = item[yAxis];
    // 分母
    const denominator = item[yAxisBar];
    // 根据原始字段名称收集分子分母数据
    if (fieldNameChainMap.yAxis && fieldNameChainMap.yAxis[yAxis]) {
      conditionData[fieldNameChainMap.yAxis[yAxis]].push({ value: item[yAxis] });
    }
    if (fieldNameChainMap.yAxisBar && fieldNameChainMap.yAxisBar[yAxisBar]) {
      conditionData[fieldNameChainMap.yAxisBar[yAxisBar]].push({
        value: item[yAxisBar],
      });
    }
    tooltip.yAxisBar = {
      yAxisName: yAxisBar,
      value: item[yAxisBar]
    };
    tooltip.yAxis = {
      yAxisName: yAxis,
      value: item[yAxis]
    };
    //产品要求最终结果保留2位小数
    const decimal = molecule && denominator ? Number(((molecule / denominator) * 100).toFixed(2)) : 0;
    percentageData.push({
      value: decimal,
      tooltip: tooltip,
    });
  });
  // 条件项存在
  if(!$isEmptyArr(yAxisConditionArr)) {
    yAxisConditionArr.forEach((yItem) => {
      const yConditionData = [];
      source.forEach((item) => {
          yConditionData.push({
            value: item[yItem],
          });
      });
      // 根据原始字段名称收集数据
      if(fieldNameChainMap['yAxisCondition'] && fieldNameChainMap['yAxisCondition'][yItem]) {
        conditionData[fieldNameChainMap['yAxisCondition'][yItem]].push(...yConditionData);
      }
    });
  }
  // 条件格式处理
  const options = {
    conditionData,
    isExistLegend: false, // 是否存在颜色维度
    seriesData: percentageData,
    legendFieldName: null,
    fieldNameChainMap,
    chartTag: 'strip',
  };
  handleConditionExtend(options, conditionOptions);
  seriesData.push({
    name: yAxis,
    data: percentageData
  })
  return {
    xAxisData,
    seriesData,
  };
}

/**
 * 排行榜条形图
 * @params datasets 数据
 * @params {object} option 多余参数
 */
export function stripRankFormat(datasets) {
  const { yAxis, xAxis, yAxisCondition } = datasets.dimensions;
  const data = deepCopy(datasets);
  if (yAxis && yAxis.includes('&')) {
    const yAxisArr = yAxis.split('&');
    data.source.forEach(item => {
      item.all = 0;
      yAxisArr.forEach(index => {
        item.all += item[index];
      })
    })
    data.source.sort((a, b) => {
      return b.all - a.all;
    });
  } else {
    data.source.sort((a, b) => {
      return b[yAxis] - a[yAxis];
    });
  }
  const xAxisData = processXAis(data);
  const seriesData = processSeriesData(data, {
    xData: xAxisData,
    yAxis,
    xAxis,
    yAxisCondition,
    dataFormatType: 'yAxis',
    barStack: 'name'
  });
  return {
    seriesData,
    xAxisData,
  };
}

// 正负条形图 数据拦截动态拼装stack
export function stripNegativeFormat(datasets, extra) {
  const { selectStackOptions } = extra;
  const { yAxis, xAxis, yAxisCondition } = datasets.dimensions;
  const legend = datasets.type === 'STATIC' ? 'name' : undefined;
  const xAxisData = processXAis(datasets);
  let seriesData = processSeriesData(datasets, {
    xData: xAxisData,
    yAxis,
    legend,
    xAxis,
    yAxisCondition,
    dataFormatType: 'yAxis',
  });
  seriesData= handleChangeGroup( seriesData, selectStackOptions) ;
  return {
    seriesData,
    xAxisData,
  };
}
